/**
 * 拥有Remote edit grid功能的表格.
 *
 * @author liuhz
 * @TODO not complete
 */
CQCIS.grid.EditGrid = Ext.extend(Ext.grid.EditorGridPanel, {
    /**
     * @cfg {dwr} DWR call DWR调用方法
     *
     * <pre>
     * ---------------------------
     * call     list call 列表显示
     * save     save call 保存
     * </pre>
     */
    /**
     * @cfg {boolean} initload default:false
     */
    /**
     * @cfg {metas} Records Metas
     *
     * <pre>
     * ------recordHeaders----------
     * id       ID
     * field    字段
     * desc     描述
     * type     字段类型
     * mapping  映射
     * renderer 渲染方法
     * ------fieldMetas-------------
     * sort     [true, false],
     * hidden   [true, false],
     * width    width (xx)
     * filter   [true, false]
     * </pre>
     */
    layout: 'table',
    autoScroll: true,
    filter: {},
    like: {},
    
    initComponent: function(){
        this.init();
        
        if (this.tbar && (this.tbar == true || this.tbar == 'true')) {
            this.tbar = ['-'];
        }
        if (!this.tbar && this.filterFields) {
            this.tbar = ['-'];
        }
        
        if (this.bbar && (this.tbar == true || this.bbar == 'true')) {
            this.bbar = new Ext.PagingToolbar({
                store: this.ds,
                pageSize: 20,
                displayInfo: true
            });
        }
        
        CQCIS.grid.EditGrid.superclass.initComponent.call(this);
        
        if (this.initConfig) {
            delete this.initConfig;
        }
        
        this.addEvents({
            /**
             * @event on delete
             */
            "beforedelete": true,
            "deleteafter": true
        });
    },
    
    init: function(){
        if (this.cm && this.ds) {
            return;
        }
        
        var columnHeaders = new Array();
        var recordHeaders = new Array();
        var filterFields = new Array();
        for (var i = 0; i < this.metas.length; i++) {
            var meta = this.metas[i], f = {}, r = {};
            /* columnHeader */
            f.header = meta.desc;
            f.dataIndex = meta.field;
            if (meta.id) {
                f.id = meta.id;
            }
            if (meta.width) {
                f.width = meta.width;
            }
            if (meta.hidden) {
                f.hidden = meta.hidden;
            }
            f.sortable = meta.sort ? meta.sort : false;
            if (meta.renderer) {
                f.renderer = meta.renderer;
            }
            
            columnHeaders[columnHeaders.length] = f;
            
            /* recordHeader */
            r.name = meta.field;
            r.type = meta.type ? meta.type : 'string';
            if (meta.mapping) {
                r.mapping = meta.mapping;
            }
            recordHeaders[recordHeaders.length] = r;
            
            /* filterField */
            if (meta.filter) {
                filterFields[filterFields.length] = {
                    text: meta.desc,
                    value: meta.field
                };
            }
        }
        
        if (filterFields.length) {
            this.filterFields = filterFields;
        }
        
        this.cm = new Ext.grid.ColumnModel(columnHeaders);
        if (this.sortable) {
            this.cm.defaultSortable = this.sortable;
        }
        
        this.ds = new Ext.data.Store({
            proxy: new Ext.data.DWRProxy(this.dwr.call, this.dwr.config),
            reader: new Ext.data.DWRReader(this.pagemeta, recordHeaders),
            remoteSort: true
        });
        
        delete this.metas;
    },
    
    onRender: function(ct, position){
        CQCIS.grid.EditGrid.superclass.onRender.call(this, arguments);
        
        if (this.initload) {
            this.store.load();
        }
        
        this.store.on('beforeload', function(){
            this.store.baseParams = {
                filter: this.filter,
                like: this.like
            };
        }, this);
    },
    
    /**
     * 刷新Grid,加载数据
     */
    reload: function(){
        var a = arguments || {};
        a.params = Ext.applyIf(a.params || {}, {
            start: 0
        });
        this.store.reload(a);
    },
    
    /**
     * 在TOP工具栏增加组件
     */
    addTop: function(){
        var a = arguments, l = a.length;
        var tbar = this.getTopToolbar();
        for (var i = 0; i < l; i++) {
            tbar.add(a[i]);
        }
    },
    
    /**
     * 在BOTTOM工具栏增加组件
     */
    addBottom: function(){
        var a = arguments, l = a.length;
        var bbar = this.getBottomToolbar();
        for (var i = 0; i < l; i++) {
            bbar.add(a[i]);
        }
    },
    
    /**
     * 返回选定行数据
     */
    getSelectedRow: function(){
        return this.getSelectionModel().getSelected() ? this.getSelectionModel().getSelected().data : null;
    }
});

Ext.reg('egrid', CQCIS.grid.EditGrid);
